import { defineStore } from 'pinia';
import { RouterState } from './interface';
import { getMenuList } from '@/uni_modules/dx-admin/api/index';
import { arrayToTree, findParentItem } from '@/uni_modules/dx-admin/libs/utils';

export const useRouteStore = defineStore('routeInfo', {
	state: (): RouterState => ({
		currentActiveRoute: '',
		routerList: [],
		tagRouteList: [],
		menuList: [],
		breadcrumbList: [],
		homePage: '/pages/index/index',
	}),
	actions: {
		setActiveRoute(router: string) {
			this.currentActiveRoute = router;
		},
		getActiveRoute() {
			return this.currentActiveRoute;
		},
		setRouterList(list: any) {
			this.routerList = list;
			if (!this.tagRouteList.length) {
				this.setTagRouteList(this.currentActiveRoute);
				this.setBreadcrumbList(this.currentActiveRoute);
			}
		},
		setTagRouteList(path: string) {
			if (~this.tagRouteList.findIndex((item) => item.url === path)) return;
			let router = this.routerList.find((item) => item.url === path);
			if (!router) return;
			// 第一个设置默认路由
			if (!this.tagRouteList.length && router.url !== this.homePage) {
				let defaultRouter = this.routerList.find((item) => item.url === this.homePage);
				if (defaultRouter) {
					this.tagRouteList.push(defaultRouter);
				}
			}
			this.tagRouteList.push(router);
		},
		removeTagRouteList(path: string, operate?: string) {
			// 如果是关闭所有，清空第一个后面的所有
			if (operate === 'closeAll') {
				this.tagRouteList.splice(1);
				if (this.currentActiveRoute !== this.homePage) {
					uni.navigateTo({ url: this.homePage });
				}
				return;
			}
			// 如果是关闭其他，清空除第一个和当前路由外的所有
			if (operate === 'closeOther') {
				if (path == this.homePage) {
					this.tagRouteList = [this.tagRouteList[0]];
					if (this.currentActiveRoute !== this.homePage) {
						uni.navigateTo({ url: this.homePage });
					}
				} else {
					this.tagRouteList = [this.tagRouteList[0], this.tagRouteList.find((item) => item.url === path) as any];
					if (path !== this.currentActiveRoute) {
						uni.navigateTo({ url: path });
					}
				}
				return;
			}
			// 如果是关闭右侧，清空当前路由后面的所有
			if (operate === 'closeRight') {
				const index = this.tagRouteList.findIndex((item) => item.url === path);
				if (~index) {
					this.tagRouteList.splice(index + 1);
				}
				if (this.currentActiveRoute !== path) {
					uni.navigateTo({ url: path });
				}
				return;
			}
			const routeLen = this.tagRouteList.length;
			const index = this.tagRouteList.findIndex((item) => item.url === path);
			if (~index) {
				// tagRoute 大于1，且删除的是第一个，且当前路由不是首页 或者  当前路由是首页，且删除的是第一个
				if ((routeLen > 1 && index === 0 && path !== this.homePage) || (path === this.homePage && index === 0)) {
					return;
				}
				this.tagRouteList.splice(index, 1);
				if (this.currentActiveRoute === path) {
					uni.navigateTo({ url: this.tagRouteList[index - 1].url });
				}
			}
		},
		setBreadcrumbList(path: string) {
			let router = this.routerList.find((item) => item.url === path);
			if (!router) return;
			let breadcrumbList: any = [router];
			findParentItem(router.parent_id, this.routerList, breadcrumbList);
			this.breadcrumbList = [...breadcrumbList];
		},
		// 获取菜单
		getMenuList() {
			getMenuList().then((res: any) => {
				if (res?.code === 1000) {
					let menuList = res.data;
					if (process.env.NODE_ENV === 'development') {
						menuList = [...menuList,{
							menu_id: 'crud',
							parent_id: '',
							name: 'CRUD代码生成',
							url: '/uni_modules/dx-admin/pages/crud/crud',
							icon: 'code-one',
						}]
					} 
					this.menuList = arrayToTree(menuList, 'menu_id', 'parent_id');
					this.setRouterList(menuList);
				}else if(res?.message){
					ElMessage.error(res?.message);
				}
			});
		},
	},
});
